<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" @clear="clearCarSearch" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" @clear="clearCarSearch" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus">
        <el-option
          v-for="item in statusList"
          :key="item.value"
          :label="item.text"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" class="search-btn" @click="searchList">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/car/addCardmonth')">添加月卡</el-button>
      <el-button @click="deleteCards">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">

      <el-table style="width: 100%" :data="carList" @selection-change="selectCheckbox">
        <el-table-column
          type="selection"
          width="55"
        />
        <!-- type='index' 这一规则会自动给页面数据加序号，但每个界面都是从一开始
             如果要进行自定义索引，写入:index="index计算方法"
        -->
        <el-table-column type="index" :index="indexCount" label="序号" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态" prop="cardStatus" :formatter="formatterStatus" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <!-- 插槽的作用： 1.占位 2.传值 -->
            <el-button size="mini" type="text" @click="addTime(scope.row.id)">续费</el-button>
            <el-button size="mini" type="text" @click="showCard(scope.row.id)">查看</el-button>
            <el-button size="mini" type="text" @click="editCard(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="deleteCard(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <!-- current-page：表示当前页码 page-sizes：可选择的每页的页容量
           page-size：当前页的容量，page-size的值通常和page-size的第一个值保持一致
           layout：分页的样式 total：一共有多少条 size-change 也容量发生b
      -->
      <el-pagination
        :current-page="params.page"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="SizeChange"
        @current-change="CurrentChange"
      /></div>
    <!-- 添加楼宇 -->
    <el-dialog
      title="添加楼宇"
      width="580px"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form> -->
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { deleteCardMonthAPI, getCardListAPI } from '@/api/carCard'
export default {
  name: 'CardMonths',
  data() {
    return {
      params: {
        page: 1,
        pageSize: 5,
        carNumber: '',
        personName: '',
        cardStatus: null // axios中的机制，如果用户传入的是null，则不会将其传入到后端
      },
      carList: [],
      total: null,
      statusList: [
        {
          text: '全部',
          value: null
        },
        {
          text: '可用',
          value: 0
        },
        {
          text: '已过期',
          value: 1
        }
      ],
      selectedValue: []
    }
  },
  created() {
    this.getCardList()
  },
  methods: {
    // 这种将接口在封装一边是为了防止在created中调用的时候，函数过多不好维护
    async getCardList() {
      const res = await getCardListAPI(this.params)
      this.carList = res.data.rows
      this.total = res.data.total
      // console.log(res)
    },
    // 月卡状态信息
    formatterStatus(row, column, cellValue, index) {
      // 这种方法有一定效果，但如果状态码过多时，会出现一定的代码冗余
      // if (cellValue === 0) {
      //   return '已过期'
      // } else {
      //   return '可用'
      // }

      // 优化后的方法,可以添加多种状态
      const statusList = {
        0: '可用',
        1: '已过期'
      }
      // console.log(cellValue)

      return statusList[cellValue]
    },
    // 月卡页面选择
    SizeChange(value) {
      this.params.pageSize = value
      this.getCardList()
    },
    // 月卡容量选择
    CurrentChange(value) {
      this.params.page = value
      this.getCardList()
    },
    // 月卡页面序号设置
    indexCount(index) {
      return index + this.params.pageSize * this.params.page - this.params.pageSize + 1
    },
    // 重置月卡页码后再去重新查询数据
    searchList() {
      // 先重置页码再去查询数据
      this.params.page = 1
      this.getCardList()
    },
    // 点击输入框x号时，清空输入框的同时，重新渲染列表
    clearCarSearch() {
      this.getCardList()
    },
    // 显示月卡信息
    showCard(id) {
      this.$router.push({
        path: '/car/addCardmonth',
        query: {
          id
        }
      })
    },
    // 编辑月卡信息
    editCard(id) {
      this.$router.push({
        path: '/car/addCardmonth',
        query: {
          id
        }
      })
    },
    // 删除月卡信息
    deleteCard(id) {
      this.$confirm('您确定要删除该信息吗?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // console.log(this.carList.length)
        if (this.carList.length === 1 && this.params.page > 1) {
          this.page--
        }
        await deleteCardMonthAPI(id)
        this.$message.success('删除成功!')
        this.getCardList()
      }).catch(() => {
        // console.log(this.carList.length)
        this.$message.error('删除失败!')
        this.getCardList()
      })
    },
    // 批量删除月卡时，复选框选中的数据
    selectCheckbox(value) {
      console.log(value)
      this.selectedValue = value
    },
    // 批量删除数据
    deleteCards() {
      // console.log(this.selectedValue)
      if (this.selectedValue.length <= 0) {
        this.$message.warning('请先选择要删除的数据')
        return
      }
      const ids = this.selectedValue.map(item => {
        return item.id
      })
      this.$confirm('你确定要删除这些数据吗', '温馨提示').then(async() => {
        await deleteCardMonthAPI(ids.join(','))
        this.$message.success('批量删除成功！')
        this.getCardList()
      }).catch(() => {

      })
    },
    // 补充功能：月卡续费
    addTime(id) {
      this.$router.push({
        path: '/car/addCardmonth',
        query: {
          id
        }
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
